package com.koi.system.controller.role;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koi.common.anno.KoiLog;
import com.koi.common.enums.log.OperType;
import com.koi.common.ex.koi.YUtil;
import com.koi.common.satoken.KoiLoginUtil;
import com.koi.model.bo.element.SelectLongBo;
import com.koi.model.vo.system.SysRoleVo;
import com.koi.pojo.system.SysRole;
import com.koi.system.service.role.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author ･ᴗ･
 * @description [角色信息表-控制层]
 */
@Validated
@RequestMapping("/koi")
@RequiredArgsConstructor
@RestController
public class SysRoleController {

    private final SysRoleService sysRoleService;

    /**
     * @description [分页查询]
     * @author ･ᴗ･
     */
    @SaCheckPermission("system:role:list")
    @GetMapping("/sysRole/listPage")
    public IPage<SysRole> listPage(SysRoleVo vo) {
        // 分页构造器
        Page<SysRole> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        // 条件构造器
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(vo.getRoleName()), SysRole::getRoleName, vo.getRoleName());
        wrapper.eq(StringUtils.isNotBlank(vo.getRoleStatus()), SysRole::getRoleStatus, vo.getRoleStatus());
        wrapper.like(StringUtils.isNotBlank(vo.getRoleCode()), SysRole::getRoleCode, vo.getRoleCode());
        // 进行分页查询
        page = sysRoleService.page(page, wrapper);
        return page;
    }

    /**
     * @description [查询所有]
     * @author ･ᴗ･
     */
    @GetMapping("/sysRole/list")
    public List<SysRole> list() {
        return sysRoleService.list();
    }

    /**
     * @description [根据主键查询]
     * @author ･ᴗ･
     */
    @GetMapping("/sysRole/getById/{id}")
    public SysRole getById(@PathVariable("id") Long id) {
        return sysRoleService.getById(id);
    }

    /**
     * @description [新增]
     * @author ･ᴗ･
     */
    @KoiLog(title = "角色新增", operType = OperType.ADD)
    @SaCheckPermission("system:role:add")
    @PostMapping("/sysRole/add")
    public void add(@Validated @RequestBody SysRole sysRole) {
        if (StringUtils.isNotBlank(KoiLoginUtil.getUserName())) {
            sysRole.setCreateBy(KoiLoginUtil.getUserName());
        }
        YUtil.isTrue(!sysRoleService.save(sysRole), "新增失败，请稍后重试");
    }

    /**
     * @description [修改]
     * @author ･ᴗ･
     */
    @KoiLog(title = "角色修改", operType = OperType.UPDATE)
    @SaCheckPermission("system:role:update")
    @PostMapping("/sysRole/update")
    public void update(@Validated @RequestBody SysRole sysRole) {
        if (StringUtils.isNotBlank(KoiLoginUtil.getUserName())) {
            sysRole.setUpdateBy(KoiLoginUtil.getUserName());
        }
        YUtil.isTrue(!sysRoleService.updateById(sysRole), "修改失败，请稍后重试");
    }

    /**
     * @description [删除]
     * @author ･ᴗ･
     */
    @KoiLog(title = "角色删除", operType = OperType.DELETE)
    @SaCheckPermission("system:role:delete")
    @PostMapping("/sysRole/deleteById/{id}")
    public void delete(@PathVariable("id") Long id) {
        YUtil.isTrue(Objects.equals(id, 1L), "超级管理员不可删除");
        YUtil.isTrue(!sysRoleService.removeById(id), "删除失败，请稍后重试");
    }

    /**
     * @description [批量删除]
     */
    @KoiLog(title = "角色批量删除", operType = OperType.DELETE)
    @SaCheckPermission("system:role:delete")
    @PostMapping("/sysRole/batchDelete")
    public void batchDelete(@RequestBody List<Long> ids) {
        for (Long id : ids) {
            YUtil.isTrue(Objects.equals(id, 1L), "超级管理员不可删除");
        }
        YUtil.isTrue(!sysRoleService.removeBatchByIds(ids), "批量删除失败，请稍后重试");
    }

    /**
     * @description [修改状态]
     * @author ･ᴗ･
     */
    @PostMapping("/sysRole/updateStatus/{roleId}/{roleStatus}")
    public void update(@PathVariable("roleId") Long roleId, @PathVariable("roleStatus") String roleStatus) {
        UpdateWrapper<SysRole> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("role_status", roleStatus).eq("role_id", roleId);
        YUtil.isTrue(!sysRoleService.update(null, updateWrapper), "修改失败，请稍后重试");
    }

    /**
     * @description [查询所有正常角色-穿梭框]
     * @author ･ᴗ･
     */
    @SaCheckPermission("system:user:role")
    @GetMapping("/sysRole/listNormalRole/{userId}")
    public Map<String, Object> listNormalRole(@PathVariable("userId") Long userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("data1", sysRoleService.listLeftRole());
        map.put("data2", sysRoleService.listRightRole(userId));
        return map;
    }

    /**
     * @description [根据当前用户ID分配角色-穿梭框]
     * @author ･ᴗ･
     */
    @KoiLog(title = "分配角色", operType = OperType.AUTH)
    @SaCheckPermission("system:user:role")
    @GetMapping("/sysRole/assignUserRole/{userId}/{roleIds}")
    public void assignUserRole(@PathVariable("userId") Long userId, @PathVariable("roleIds") List<Long> roleIds) {
        sysRoleService.assignUserRole(userId, roleIds);
    }

    /**
     * @description [获取角色下拉框]
     * @author ･ᴗ･
     */
    @GetMapping("/sysRole/listRoleElSelect")
    public List<SelectLongBo> listRoleElSelect() {
        return sysRoleService.listRoleElSelect();
    }

}
